package services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import play.mvc.Finally;
import utils.BizConstants;

import com.aton.db.SessionFactory;

import models.Device;
import models.DeviceHealth;
import models.DeviceStatus;
import models.SystemParams;
import models.mappers.DeviceHealthMapper;
import models.mappers.DeviceMapper;
import models.mappers.ShopMapper;
import notifiers.MailUtils;
import vos.DeviceSearchVo;
import vos.DeviceVo;

public class DeviceService {
	/**
	 * 根据条件统计设备数量
	 *
	 * @Title: countDevice 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countDevice(DeviceSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
            return mapper.countDevice(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据条件查询设备列表
	 *
	 * @Title: listDevice 
	 * @param vo
	 * @return
	 * @return: List<Device>
	 */
	public static List<DeviceVo> listDevice(DeviceSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
            return mapper.listDevice(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 批量启用
	 *
	 * @Title: betchEnable 
	 * @param ids 选中记录id
	 * @param operatorid 
	 * @return: void
	 */
	public static Boolean betchEnable(List<String> listid, String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
            mapper.betchEnable(map);
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量停用
	 *
	 * @Title: betchEnable 
	 * @param listid 选中记录id
	 * @return: void
	 */
	public static Boolean betchDisable(List<String> listid,String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
            mapper.betchDisable(map);
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchDel 
	 * @param ids
	 * @return
	 * @return: boolean
	 */
	public static boolean betchDel(String ids) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", listid);
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
            mapper.betchDel(map);
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 删除设备
	 *
	 * @Title: delDevice 
	 * @param id 设备id
	 * @return
	 * @return: boolean
	 */
	public static boolean delDevice(String id) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
	        mapper.delDevice(id);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		
		return true;
	}
	/**
	 * 验证编号是否存在
	 *
	 * @Title: checkCode 
	 * @param deviceid 设备编号
	 * @return
	 * @return: boolean
	 */
	public static boolean checkCode(String deviceid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
	        String device= mapper.checkCode(deviceid);
	        if(StringUtils.isEmpty(device)){
	        	return false;
	        }
	        else{
	        	return true;
	        }
		}catch(Exception e){
			e.printStackTrace();
			return true;
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据id获取设备信息
	 *
	 * @Title: getById 
	 * @param deviceid 设备id
	 * @return
	 * @return: Device
	 */
	public static Device getById(String deviceid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
			return mapper.getById(deviceid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存设备信息
	 *
	 * @Title: saveDevice 
	 * @param device 设备对象
	 * @return
	 * @return: boolean
	 */
	public static boolean saveDevice(Device device) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
			Date date=new Date();
			device.createtime=date;
	        mapper.saveDevice(device);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 更新设备信息
	 *
	 * @Title: updateDevice 
	 * @param device
	 * @return
	 * @return: boolean
	 */
	public static boolean updateDevice(Device device) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
			Date date=new Date();
			device.modifytime=date;
	        mapper.updateDevice(device);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 根据经销商id集合查询设备集合
	 *
	 * @Title: selectByDealers 
	 * @param listid
	 * @return
	 * @return: List<Shop>
	 */
	public static List<String> selectByDealers(List<String> listid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", listid);
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
			return mapper.selectByDealers(map);
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据门店集合查询门店集合下的设备
	 *
	 * @Title: selectByShops 
	 * @param listid
	 * @return
	 * @return: List<String>
	 */
	public static List<String> selectByShops(List<String> listid) {
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", listid);
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
			return mapper.selectByShops(map);
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据门店id获取设备列表
	 *
	 * @Title: findByShop 
	 * @param id
	 * @return
	 * @return: Device
	 */
	public static List<Device> findByShop(String id) {
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
			return mapper.findByShop(id);
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据id获取设备
	 *
	 * @Title: getViewById 
	 * @param id
	 * @return
	 * @return: DeviceVo
	 */
	public static DeviceVo getViewById(String id) {
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
			return mapper.getViewById(id);
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取设备状态
	 *
	 * @Title: getStatus 
	 * @return
	 * @return: List<DeviceStatus>
	 */
	public static List<DeviceStatus> getStatus() {
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
			return mapper.getStatus();
		}finally{
			ss.close();
		}
	}
	/**
	 * 更新设备列表健康状态
	 *
	 * @Title: updateDeviceStatus 
	 * @param hraids
	 * @return: void
	 */
	public static void updateDeviceStatus(List<String> hraids,String email) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		List<DeviceVo> notOkIds=new ArrayList<DeviceVo>();
		try{
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", hraids);
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
			if(hraids!=null&&hraids.size()>0){
				mapper.updateDeviceStatusOk(map);
			}
			mapper.updateDeviceStatusNotOk(map);
			//获取不健康记录
			notOkIds=DeviceService.getNotIn(hraids);
			if(notOkIds!=null&&notOkIds.size()>0){
				//保存设备不健康记录
				List<DeviceHealth> healths=new ArrayList<DeviceHealth>();
				for(DeviceVo device:notOkIds){
					DeviceHealth health=new DeviceHealth(device.deviceid,new Date(),"当日设备未上传数据",2);
					healths.add(health);
				}
				DeviceHealthService.save(healths);
			}
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
		}
		finally{
			ss.close();
		}
		if(notOkIds!=null&&notOkIds.size()>0){
			MailUtils.sendDeviceStatus(notOkIds,email);
		}
	}
	/**
	 * 根据门店统计门店下的设备
	 *
	 * @Title: countDeviceByShop 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countDeviceByShop(DeviceSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
            return mapper.countDeviceByShop(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据门店获取门店下设备列表
	 *
	 * @Title: getDeviceByShop 
	 * @param vo
	 * @return
	 * @return: List<Device>
	 */
	public static List<DeviceVo> getDeviceByShop(DeviceSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
            return mapper.getDeviceByShop(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 导出excel
	 *
	 * @Title: exportXls 
	 * @param vo
	 * @return
	 * @return: List<DeviceVo>
	 */
	public static List<DeviceVo> exportXls(DeviceSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
            return mapper.exportXls(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取下拉列表
	 *
	 * @Title: getCombobox 
	 * @return
	 * @return: List<Device>
	 */
	public static List<Device> getCombobox() {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
            return mapper.getCombobox();
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取没有在list中的id集合
	 *
	 * @Title: getNotIn 
	 * @param hraids
	 * @return
	 * @return: List<String>
	 */
	public static List<DeviceVo> getNotIn(List<String> hraids) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			DeviceMapper mapper = ss.getMapper(DeviceMapper.class);
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("lists", hraids);
            return mapper.getNotIn(map);
        } finally {
            ss.close();
        }
	}
}
